<template>
  <div>
    <el-breadcrumb separator="/">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>订单管理</el-breadcrumb-item>
      <el-breadcrumb-item>订单列表</el-breadcrumb-item>
    </el-breadcrumb>

    <el-card class="box-card">
      <el-row :gutter="20">
        <el-col :span="8">
          <el-input
            placeholder="请输入内容"
            v-model="odersParams.query"
            class="input-with-select"
          >
            <el-button
              slot="append"
              icon="el-icon-search"
              @click="getOders"
            ></el-button>
          </el-input>
        </el-col>
        <el-col :span="16"> </el-col>
      </el-row>
      <el-table :data="odersList" border style="width: 100%">
        <el-table-column type="index" label="#" width="80"> </el-table-column>
        <el-table-column prop="order_number" label="订单编号" width="300">
        </el-table-column>
        <el-table-column prop="order_price" label="订单价格" width="150">
        </el-table-column>
        <el-table-column prop="is_send" label="是否付款"> </el-table-column>
        <el-table-column prop="order_pay" label="是否发货">
          <template slot-scope="scope">
            <el-tag type="success" v-if="scope.row.order_pay == 1"
              >已付款</el-tag
            >
            <el-tag type="warning" v-else>未付款</el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="create_time" label="下单时间">
          <template slot-scope="scope">
            {{ scope.row.create_time | dateFormat }}
          </template>
        </el-table-column>
        <el-table-column label="操作" width="250">
          <template slot-scope="scope">
            <el-button
              type="primary"
              icon="el-icon-edit"
              @click="editOrder(scope.row)"
            ></el-button>
            <el-button
              type="success"
              icon="el-icon-location"
              @click="seeOrder(scope.row)"
            ></el-button>
          </template>
        </el-table-column>
      </el-table>

      <el-pagination
        background
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="odersParams.pagenum"
        :page-sizes="[5, 6, 8, 10]"
        :page-size="odersParams.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>

      <el-dialog
        title="修改订单地址"
        :visible.sync="editVisible"
        width="50%"
        @close="resetForm"
      >
        <el-form
          ref="editFormRef"
          :model="editForm"
          label-position="top"
          :rules="rules"
        >
          <el-form-item label="省市区/县" prop="editCity">
            <el-cascader
              v-model="editForm.editCity"
              :options="cityData"
              :props="{ expandTrigger: 'hover' }"
              @change="handleChange"
            ></el-cascader>
          </el-form-item>
          <el-form-item label="详细地址" prop="editAddress">
            <el-input v-model="editForm.editAddress"></el-input>
          </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
          <el-button @click="editVisible = false">取 消</el-button>
          <el-button type="primary" @click="editPath">确 定</el-button>
        </span>
      </el-dialog>

      <el-dialog title="查看物流信息" :visible.sync="seeVisible" width="50%">
        <el-timeline >
          <el-timeline-item
            v-for="(activity, index) in activities"
            :key="index"
            :timestamp="activity.time"
          >
            {{ activity.context }}
          </el-timeline-item>
        </el-timeline>
        <span slot="footer" class="dialog-footer">
          <el-button @click="seeVisible = false">取 消</el-button>
          <el-button type="primary" @click="seeVisible = false"
            >确 定</el-button
          >
        </span>
      </el-dialog>
    </el-card>
  </div>
</template>

<script>
import cityData from "./citydata.js";
export default {
  data() {
    return {
      activities: [],
      order_id: 0,
      editVisible: false,
      seeVisible: false,
      odersParams: {
        query: "",
        pagenum: 1,
        pagesize: 10,
      },
      total: 0,
      odersList: [],
      editForm: {
        editCity: [],
        editAddress: "",
      },
      cityData,
      rules: {
        editCity: [{ required: true, message: "请输入地址", trigger: "blur" }],
        editAddress: [
          { required: true, message: "请输入详细地址", trigger: "blur" },
        ],
      },
    };
  },
  created() {
    this.getOders();
  },
  methods: {
    //获取订单列表数据
    async getOders() {
      const { data: res } = await this.$http.get("orders", {
        params: this.odersParams,
      });
      console.log(res.data.goods);
      if (res.meta.status !== 200)
        return this.$message.error("订单列表请求失败");
      this.odersList = res.data.goods;
      this.total = res.data.total;
      //   console.log(this.odersParams.query);
      //   console.log(this.cityData);
    },

    //切换订单页面
    handleCurrentChange(newPagenum) {
      this.odersParams.pagenum = newPagenum;
      this.getOders();
      // console.log(newPagenum)
    },

    //切换订单数量
    handleSizeChange(newPagesize) {
      this.odersParams.pagesize = newPagesize;
      this.getOders();
      // console.log(newPagesize)
    },

    //点击修改订单地址
    editOrder(data) {
      this.order_id = data.order_id;
      this.editVisible = true;
      console.log(this.order_id);
    },

    //点击查看物流信息
    async seeOrder() {
      const { data: res } = await this.$http.get("kuaidi/1106975712662");
      if (res.meta.status !== 200)
        return this.$message.error("获取订单信息失败");
      this.activities = res.data;
      this.seeVisible = true;
      
    },

    handleChange(newValue) {
      console.log(newValue);
    },

    //重置表单
    resetForm() {
      this.$refs.editFormRef.resetFields();
    },

    //发送修改订单请求
    editPath() {
      this.$refs.editFormRef.validate(async (valid) => {
        if (!valid) return this.$message.error("订单地址输入有误");
        const { data: res } = await this.$http.put("orders/" + this.order_id);
      });

      this.editVisible = false;
    },
  },
};
</script>

<style scoped>
.el-card {
  margin-top: 20px;
}

.el-pagination {
  margin-top: 20px;
}
</style>
